package com.djk;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;

import java.util.List;

//自定义单词列表的适配器
public class WordAdapter extends ArrayAdapter<Word> {
    private Context mContext;
    private List<Word> mWordList;

    public WordAdapter(Context context, List<Word> wordList) {
        super(context, 0, wordList);
        mContext = context;
        mWordList = wordList;
    }

    //设置每一项视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            //获取列表项的子视图
            listItem = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        }
        //获取当前点击的位置
        Word currentWord = mWordList.get(position);

        TextView textWord = listItem.findViewById(R.id.textWord);
        textWord.setText(currentWord.getWord());

        TextView textMeaning = listItem.findViewById(R.id.textMeaning);
        textMeaning.setText(currentWord.getMeaning());
        //给删除按钮设置一个点击的监听器
        ImageButton deleteButton = listItem.findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理删除操作
                int wordId = currentWord.getId();
                deleteWord(wordId);
            }
        });

        // 添加双击事件监听器
        GestureDetector gestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                // 处理双击事件
                showEditDialog(currentWord); // 弹出修改信息的对话框
                return true;
            }
        });

        listItem.setOnTouchListener((v, event) -> {
            gestureDetector.onTouchEvent(event);
            return true;
        });

        return listItem;
    }

    // 展示编辑对话框
    private void showEditDialog(Word word) {
        // 创建自定义对话框来修改信息
        // 使用 AlertDialog来编辑单词信息
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_edit, null);
        dialogBuilder.setView(dialogView);
        EditText editTextWord = dialogView.findViewById(R.id.editTextWord);
        editTextWord.setText(word.getWord());

        EditText editTextMeaning = dialogView.findViewById(R.id.editTextMeaning);
        editTextMeaning.setText(word.getMeaning());

        dialogBuilder.setTitle("编辑单词")
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取编辑后的内容，然后更新数据库
                        String editedWord = editTextWord.getText().toString();
                        String editedMeaning = editTextMeaning.getText().toString();

                        updateWord(word.getId(), editedWord, editedMeaning);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();
    }

    // 更新单词信息
    private void updateWord(int wordId, String editedWord, String editedMeaning) {
        WordDao wordDao = new WordDao(mContext);
        boolean success = wordDao.updateWord(new Word(wordId, editedWord, editedMeaning));

        if (success) {
            mWordList.clear();
            mWordList.addAll(wordDao.getAllWords());
            notifyDataSetChanged();
            Toast.makeText(mContext, "更新成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "更新失败", Toast.LENGTH_SHORT).show();
        }
    }

    // 删除单词的方法
    private void deleteWord(int wordId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("确认删除")
                .setMessage("确定要删除此单词吗？")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 用户点击确认按钮时执行的操作
                        performDelete(wordId);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 用户点击取消按钮时执行的操作（可选）
                        dialogInterface.dismiss(); // 关闭对话框
                    }
                })
                .show();
    }

    private void performDelete(int wordId) {
        WordDao wordDao = new WordDao(mContext);
        boolean success = wordDao.deleteById(wordId);
        if (success) {
            mWordList.clear();
            mWordList.addAll(wordDao.getAllWords());
            notifyDataSetChanged();
            Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "删除失败", Toast.LENGTH_SHORT).show();
        }
    }

}



